﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;

namespace EntityStorage
{
    public class BaseEntityStorage
    {
        public DataRow ExecuteOne(string queryName, CommandType commandType, params SqlParameter[] parameters)
        {
            DataTable table = ExecuteMany(queryName, commandType, parameters);

            if (table.Rows.Count > 0)
                return table.Rows[0];

            return null;
        }

        protected DataRow ExecuteOne(string storedProcedureName, params SqlParameter[] parameters)
        {
            return ExecuteOne(storedProcedureName, CommandType.StoredProcedure, parameters);
        }

        protected DataRow ExecuteOne(string storedProcedureName)
        {
            return ExecuteOne(storedProcedureName, null);
        }


        protected DataTable ExecuteMany(string storedProcedureName)
        {
            return ExecuteMany(storedProcedureName, null);
        }

        protected DataTable ExecuteMany(string storedProcedureName, params SqlParameter[] parameters)
        {
            return ExecuteMany(storedProcedureName, CommandType.StoredProcedure, parameters);
        }

        protected DataTable ExecuteMany(string queryString, CommandType commandType ,params SqlParameter[] parameters)
        {
            using (SqlConnection con = Factories.ConnectionFactory.Create())
            {
                using (SqlCommand cmd = new SqlCommand(queryString, con))
                {
                    if (parameters != null && parameters.Length > 0)
                        cmd.Parameters.AddRange(parameters);

                    cmd.CommandType = commandType;

                    DataTable dt = new DataTable();
                    dt.Load(cmd.ExecuteReader());

                    return dt;
                }
            }
        }


        protected DataSet ExecuteDataSet(string storedProcedure)
        {
            DataSet ds = new DataSet();

            using (SqlConnection con = EntityGenerator.EntityStorage.Factories.ConnectionFactory.Create())
            {
                SqlDataAdapter adapter = new SqlDataAdapter();
                SqlCommand cmd = new SqlCommand(
                    storedProcedure,
                    con);

                cmd.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand = cmd;

                adapter.Fill(ds);
            }

            return ds;
        }

        //protected void ExecuteStoredComplexDataSet(DataSet ds,
        //    string storedProcedureName,
        //    string[] parameterNames,
        //    object[] parameterValues,
        //    DataTableMapping[] dataTableMappings)
        //{
        //    SqlDataAdapter adapter;
        //    SqlCommand cmd;

        //    using (SqlConnection con = EntityGenerator.EntityStorage.Factories.ConnectionFactory.Create())
        //    {

        //        adapter = new SqlDataAdapter();
        //        cmd = new SqlCommand(
        //            storedProcedureName,
        //            con);

        //        adapter.SelectCommand = cmd;
        //        adapter.TableMappings.Clear();
        //        adapter.TableMappings.AddRange(dataTableMappings);


        //        cmd.CommandType = CommandType.StoredProcedure;

        //        for (int i = 0; i < parameterNames.Length; i++)
        //        {
        //            if (parameterValues[i] == null)
        //                cmd.Parameters.AddWithValue(parameterNames[i], DBNull.Value);
        //            else
        //                cmd.Parameters.AddWithValue(parameterNames[i], parameterValues[i]);
        //        }


        //        adapter.Fill(ds);
        //    }
        //}

        public T GetValue<T>(DataRow row, string columnName)
        {
            if (row.Table.Columns.Contains(columnName))
                return row.Field<T>(columnName);
            else
                throw new ArgumentNullException("Column name " + columnName + " is not found in the DataRow row");
        }

        protected object DBNullIfNull(object o)
        {
            if (o == null)
                return DBNull.Value;
            else
                return o;
        }
    }
}